package com.tourism.storage.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.t2cn.common.dbwrap.DbWrapException;
import com.t2cn.common.dbwrap.Jdbc;
import com.tourism.storage.ConnextionProxy;
import com.tourism.storage.pojo.Partner;
import com.tourism.storage.pojo.Passengers;
import com.tourism.storage.pojo.Teams;

public class TeamsDao {

	public int addTeams(Teams pojo){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			int i = Jdbc.Insert.insert(conn, pojo);
			conn.commit();
			return i;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return 0;
	}	
	
	public int updateTeams(Teams pojo){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Teams where = new Teams();
			where.setTeamid(pojo.getTeamid());
			int i = Jdbc.Update.update(conn, pojo, where);
			conn.commit();
			return i;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return 0;
	}
	public List<Teams> getTeams(Integer status){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Teams pojo = new Teams();
			if( status >= 0 )
				pojo.setStatus(status);
			
			List<Teams> list = Jdbc.Select.select(conn, pojo);
			return list;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return null;
	}
	
	public List<Teams> getTeamsByCourse(Integer courseid, Integer status){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Teams pojo = new Teams();
			if( courseid > 0 )
				pojo.setCourseid(courseid);
			
			if( status >= 0 )
				pojo.setStatus(status);
			
			List<Teams> list = Jdbc.Select.select(conn, pojo);
			return list;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return null;
	}
	
	public Teams getTeamsById(Integer teamid){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Teams pojo = new Teams();
			pojo.setTeamid(teamid);
			List<Teams> list = Jdbc.Select.select(conn, pojo);
			return (Teams)Jdbc.Select.getFirstNode(list);
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return null;
	}
	
	public int deleteTeams(Integer teamid){
		Connection conn = null;
		try {
			conn = ConnextionProxy.getTestConnection();
			Teams where = new Teams();
			where.setTeamid(teamid);
			int i = Jdbc.Delete.delete(conn, where);
			conn.commit();
			return i;
		} catch (DbWrapException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			Jdbc.Resource.closeConnection(conn);
		}
		return 0;
	}
}
